// pages/payment/payment.js
var total_micro_second = 1;

/* 毫秒级倒计时 */
function countdown(that) {
    // 渲染倒计时时钟
    that.setData({
        clock: dateformat(total_micro_second)
    });

    if (total_micro_second <= 0) {
        that.setData({
            clock: "已经截止"
        });
        // timeout则跳出递归
        return;
    }
    setTimeout(function () {
        // 放在最后--
        total_micro_second -= 500;
        countdown(that);
    }
      , 500)
}

// 时间格式化输出，如3:25:19 86。每500ms都会调用一次
function dateformat(micro_second) {
    // 秒数
    var second = Math.floor(micro_second / 1000);
    // 小时位
    var hr = Math.floor(second / 3600 % 24);
    // 分钟位
    var min = Math.floor((second - Math.floor(second / 3600) * 3600) / 60);
    // 秒位
    var sec = (second - Math.floor(second / 3600) * 3600 - min * 60);// equal to => var sec = second % 60;

    var date = Math.floor(second / 3600 / 24)
    var data = { date, hr, min, sec }
    return data;
    }
  Page({

    /**
     * 页面的初始数据
     */
        data: {
        kp: "开",
            showFlag: true,
            //分享图片的数据
            hidden: true,
          tips:"",
          Full:false,
            img: ["../../image/a2_03.jpg", "../../image/a10_04.png", "../../image/a10_04.png"],
        },


      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
          wx.getSetting({
              success: (res) => {
                  if (!res.authSetting['scope.writePhotosAlbum']) {
                      this.setData({
                          showFlag: false
                      })
                  }
              }
          })
          var globalData = getApp().globalData;
          var custId = globalData.custID;
          var url = globalData.apiurl + '/mobile/api/TeamBuy/GetPayMent';
          var that = this;
        wx.showLoading({
          title: '加载中...',
          mask: true
        });
          wx.request({
              url: url,
              data: {
                  custId: custId,
                  unionStoreID: options.unionStoreID,
                  teamId: options.teamId,
                  orderId: options.orderId,
                  ZPAppid: globalData.zhengpinAppid,
                  smallAppId: globalData.zpAppid,
                  smallSecret: globalData.secret,
                  isAuthComponent:globalData.isAuthComponent
              },
              headers: {
                  'Content-Type': 'application/json'
              },
              success: function (res) {
                wx.hideLoading();
                  var json = JSON.parse(res.data.content);
                  total_micro_second = Date.parse(json.Team.EndTime) - Date.parse(json.Now);
                  countdown(that);
                  if (json.UserList[1] != null) {
                      that.setData({
                          kp: "拼"
                      })
                  }
            var kongqueData =[];
                for (var i = json.UserList.length + 2; i <= json.Team.TeamNumber; i++) {
                  kongqueData.push(i);
                }
               var full=false;
               var tips="";
              
                if (json.Team.Status == 2)
               {
                  full=true;
                  tips ="对不起！活动拼团人数已满！";
                } else if (json.Team.Status == 3) {
                  full = true;
                  tips="规定时间内未成团，已失效!";
                }
                  that.setData({
                    Full:full,
                    tips:tips,
                    kongque: kongqueData,
                      TeamLeader: json.TeamLeader,
                      Team: json.Team,
                      UserList: json.UserList,
                      Shop: json.Shop,
                      Product: json.Product,
                    DiscountedPrice: (json.Order.AmountPayable - json.Order.YunMoney - json.Order.GetOrderDetails[0].Price).toFixed(2),
                      options: options,
                      isShow:0,
                      Order: json.Order,
                      DeadLine: json.Deadline,
                      shareCard: json.ProductImgBase64,
                      ProductImgBase64: json.Product.ProductImg.replace('https', 'http').replace('http', 'https'),
                      HeadIMgBase: "data:image/png;base64," + json.HeadIMgBase,
                      custId: custId,
                      unionStoreID: options.unionStoreID,
                      teamId: options.teamId,
                      orderId: options.orderId
                  })
                  
               
              }
          });
      },

      sharePopk: function () {
          this.setData({
              sharepopkin: !this.data.sharepopkin
          });
      },
    
    saveImg: function () {
      var that = this;
      wx.previewImage({
        urls: [that.data.shareCard],
        success: res => {
        },
        fail: res => {

        }
      });
    },
      handler: function (e) {
          if (e.detail.authSetting["scope.writePhotosAlbum"]) {
              this.setData({
                  showFlag: true
              });

          } else {
              this.setData({
                  showFlag: true
              });
          }
      },

     linkto:function()
     {
       var that=this;
       wx.navigateTo({
         url: '/pages/teamdetails/teamdetails?productId=' + that.data.Order.GetOrderDetails[0].ProductId + "&shopID=" + that.data.Order.GetOrderDetails[0].UnionStoreID
       })
     },


      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {

      },

      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function () {

      },

      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function () {

      },

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {

      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function () {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {
          var globalData = getApp().globalData;
          var custId = globalData.custID;
          var that = this
        var title = "一起来拼团吧：" + that.data.Order.GetOrderDetails[0].ProductName;
          return {
            title: title,
            imageUrl: that.data.Order.GetOrderDetails[0].GoodsImg,
              path: '/pages/Invitation/Invitation?custId=' + custId + "&unionStoreID=" + that.options.unionStoreID + "&teamId=" + that.options.teamId
          }
      },

  })